package code.assets
{
	import code.interfaces.IContainerObject3D;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import mx.core.BitmapAsset;
	
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.scenes.Scene3D;

	public class LoadImageCubes extends LoadScene
	{
		private var _loader:Loader;
		private var _dictionary:Dictionary;
		private var _width:uint;
		private var _depth:uint;
		private var _scene:Scene3D;
		private var _numContainers:uint;
		private static var count:uint;
		
		public function LoadImageCubes(topLevel:XML, scene:Scene3D, w:uint, d:uint)
		{
			_dictionary = new Dictionary();
			_width = w,
			_depth = d;
			_scene = scene;
			_numContainers = topLevel.li.length();
			count = 0;
			
			for each(var element:XML in topLevel.li)
			{
				//scene.addChild(getContainerObject3D(element, w, d) as DisplayObject3D);
				var url:URLRequest = new URLRequest(element.a.@titleImage);
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
				_loader.load(url);
				
				_dictionary[_loader] = element;
			}
		}
		
		protected function onImageLoaded(evt:Event):void
		{
			var element:XML = _dictionary[(evt.target as LoaderInfo).loader];
			var img:DisplayObject = (evt.target as LoaderInfo).content;
			var cube:IContainerObject3D = getContainerObject3D(element, img);

			_scene.addChild(cube as DisplayObject3D);
			
			if(_numContainers == count +1)
			{
				dispatchEvent(new Event("LOADED"));
			}
			count++;
		}
		
		protected function getContainerObject3D(topLevel:XML, img:DisplayObject):IContainerObject3D
		{	
			//var clip:TextSurface;
			var object3D:IContainerObject3D;
			var ml:MaterialsList;
			var mMat:MaterialObject3D;
			var bd:BitmapData = new BitmapData(img.width, img.height);
			bd.draw(img);
			
			ml = new MaterialsList();
			for(var i:uint=0; i<6; i++)
			{	
				
				switch(i)
				{
					case 0:
					{
						mMat = new ColorMaterial(0xFFFFFF);
						ml.addMaterial(mMat, "top");
						break;
					}
					case 1:
					{
						mMat = new ColorMaterial(0xFFFFFF);
						ml.addMaterial(mMat, "bottom");
						break;
					}
					case 2:
					{
						mMat = new BitmapMaterial(bd);
						ml.addMaterial(mMat, "back");
						break;
					}
					case 3:
					{
						mMat = new BitmapMaterial(bd);
						ml.addMaterial(mMat, "right");
						break;
					}
					case 4:
					{
						mMat = new BitmapMaterial(bd);
						ml.addMaterial(mMat, "left");
						break;
					}
					case 5:
					{
						mMat = new ColorMaterial(0x99FFFF);
						ml.addMaterial(mMat, "front");
						break;
					}
				}
				
				var h:uint = img.height;
				
				mMat.interactive = true;
				mMat.smooth = true;
			}
			
			object3D = new ContainerCube3D(ml, _width, _depth, h, 6, 6, 6);
			(object3D as Cube).y = h/2;
			object3D.linkID = topLevel.a.@id;
			
			return object3D;
		}
		
	}
}